<template>
  <div class="capp-container">
    <div class="capp-heder clearfix">
      <div class="logo fl">
        <router-link :to="{ name: 'main'}">
          <img
            style="display: flex;align-items: center;justify-content: center;margin-right: 5px;"
            src="~@/assets/images/logo-capp.png"
          >
        </router-link>
        <span class="logo__wrd">创建小程序</span>
      </div>
      <div class="fr">
        <span class="text-info">{{userName}}</span>
        <span class="text-secondary">&nbsp;-&nbsp;</span>
        <router-link class="text-primary" :to="{ name: 'main'}">小程序列表</router-link>
        <span class="text-secondary">&nbsp;-&nbsp;</span>
        <a href="javascript:void(0);" class="text-primary" @click="loginOut">退出</a>
      </div>
    </div>
    <div class="capp-main">
      <div class="capp-step">
        <el-steps :active="step" simple style="margin-top: 20px">
          <el-step title="选择小程序类型" icon="el-icon-menu" description="选择小程序类型"></el-step>
          <el-step title="选择小程序版本" icon="el-icon-document" description="选择小程序版本"></el-step>
          <el-step title="完善小程序信息" icon="el-icon-edit" description="完善小程序信息"></el-step>
        </el-steps>
      </div>
      <el-row v-show="step===1" :gutter="20">
        <el-col :key="index" v-for="(item,index) in products" :span="8">
          <div class="capp-kindItem">
            <h2 style="text-align: center;">
              <i
                class="fa el-icon-tickets"
                style="color: #ff3600;font-size: 22px;margin-right: 25px;"
              ></i>
              {{item.name}}
            </h2>
            <div class="product-item">
              <div class="item-img">
                <i class="fa el-icon-star-off"></i>
              </div>
              <div class="item-text">
                <div class="item-title">适用主体</div>
                <div class="item-detail">{{item.subject}}</div>
              </div>
            </div>
            <div class="product-item">
              <div class="item-img">
                <i class="fa el-icon-share"></i>
              </div>
              <div class="item-text">
                <div class="item-title">核心场景</div>
                <div class="item-detail">{{item.scene}}</div>
              </div>
            </div>
            <div class="product-item">
              <div class="item-img">
                <i class="fa el-icon-goods"></i>
              </div>
              <div class="item-text">
                <div class="item-title">适用行业</div>
                <div class="item-detail">{{item.industry}}</div>
              </div>
            </div>
            <div class="product-item footer-btn" data-name="restaurant">
              <el-button type="danger" @click="select_product(item.id,item.code)">立即创建</el-button>
            </div>
          </div>
        </el-col>
      </el-row>
      <el-row v-show="step===2" class="capp-version" :gutter="20">
        <el-col :key="index" v-for="(item,index) in product_price" :span="8">
          <div class="pricing-list text-left">
            <div class="pricing-header bg-blue-700">
              <div class="pricing-title">{{item.type}}</div>
              <div class="pricing-price">
                <span class="pricing-currency">¥</span>
                <span class="pricing-amount">{{item.price}}</span>
                <span class="pricing-period">/ {{item.day}}天</span>
              </div>
            </div>
            <div class="pricing-footer" data-type="395">
              <el-button type="primary" @click="select_price(item.id)">创建小程序</el-button>
            </div>
          </div>
        </el-col>
      </el-row>
      <el-row v-show="step===3" class="capp-infos" :gutter="20">
        <el-form
          label-position="labelPosition"
          label-width="150px"
          :model="createForm"
          :rules="rulesForm"
          ref="createForm"
        >
          <!-- <el-col :span="24">
            <el-alert
                title="该小程序有30天免费试用期"
                type="error">
              </el-alert>
          </el-col>-->
          <el-col :span="12" :offset="6" class="infos-main">
            <el-form-item label="小程序名称" prop="name">
              <el-input v-model="createForm.name" placeholder="请输入小程序名称"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12" :offset="6" class="infos-main infos-footer">
            <el-button type="primary" @click="create_miniapp()">开通我的小程序</el-button>
          </el-col>
        </el-form>
      </el-row>
    </div>
  </div>
</template>

<script>
import API from "@/api";
import { mapMutations } from "vuex";
export default {
  data() {
    return {
      step: 1,
      products: [],
      code: "",
      createForm: {
        product_id: "",
        price_id: "",
        name: ""
      },
      product_price: [],
      rulesForm: {
        name: [
          { required: true, message: "小程序名称不能为空", trigger: "change" }
        ]
      }
    };
  },
  computed: {
    userName: {
      get() {
        return this.$store.state.user.name;
      }
    }
  },
  created() {
    this.getCAppKind();
  },
  methods: {
    getCAppKind() {
      API.miniapp.getMiniAppKind().then(({ data }) => {
        if (data.code === 0) {
          this.products = data.items;
        }
      });
    },
    select_product(id, code) {
      this.code = code;
      API.miniapp.getPriceById(id).then(({ data }) => {
        if (data.code === 0) {
          this.product_price = data.items;
          this.createForm.product_id = id;
          this.step = 2;
        }
      });
    },
    select_price(id) {
      this.createForm.price_id = id;
      this.step = 3;
      console.log(this.step);
    },
    loginOut() {
      API.common.logout().then(({ data }) => {
        if (data.code === 0) {
          this.$cookie.delete("token");
          // this.$cookie.delete('role')
          this.$router.replace({ name: "login" });
        }
      }); // 登出处理
    },
    create_miniapp() {
      this.$refs["createForm"].validate(valid => {
        if (valid) {
          var params = {
            productId: this.createForm.product_id,
            priceId: this.createForm.price_id,
            name: this.createForm.name
          };
          API.miniapp.createMiniapp(params).then(({ data }) => {
            console.log(data);
            if (data.code === 0) {
              this.$router.push({ name: "miniapp-" + this.code });
              this.$cookie.set("appid", data.appid, { expires: "3600s" });
              this.$cookie.set("apptype", this.code, { expires: "3600s" });
            } else {
              this.$message({
                type: "warning",
                message: data.msg
              });
            }
          });
        }
      });
    }
  }
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="scss">
.capp-container {
  width: 100%;
  max-width: 1024px;
  margin: 0 auto 20px;
}
.capp-heder {
  padding: 15px 0 10px;
  width: 100%;
  line-height: 35px;
}
.capp-main {
  margin: 20px 0;
  padding: 20px;
  border: 1px solid #eee;
}
.capp-step {
  margin-bottom: 20px;
}
.capp-kindItem {
  position: relative;
  background: #fff;
  width: 100%;
  border: 2px solid #fff;
  box-shadow: 0 0 1px rgba(0, 6, 122, 0.1);
  transition: all 0.1s;
  margin-bottom: 40px;
  &:hover {
    border: 2px solid #ff3600;
    box-shadow: none;
  }
}
.product-item {
  margin-top: 20px;
  display: block;
  height: 60px;
  width: 100%;
  padding: 0 10px;
}
.product-item .item-img {
  width: 55px;
  height: 60px;
  line-height: 60px;
  float: left;
  margin-right: 10px;
  text-align: center;
}
.product-item .item-img .fa {
  width: 48px;
  height: 48px;
  font-size: 32px;
  text-align: center;
  line-height: 48px;
  color: #ff3600;
}
.product-item .item-text .item-title {
  font-size: 18px;
  padding-left: 8px;
  font-family: 黑体;
}
.product-item .item-text .item-detail {
  font-size: 14px;
  padding-left: 8px;
  padding-top: 4px;
  color: #999;
}
.footer-btn.product-item {
  text-align: center;
  padding: 20px 0 10px;
  border-bottom: none;
  box-sizing: content-box;
}
.pricing-list [class*="bg-"],
.pricing-list [class*="bg-"] *,
.pricing-list [class^="bg-"],
.pricing-list [class^="bg-"] * {
  color: #fff;
}
.pricing-list {
  color: #fff;
  margin-bottom: 22px;
  text-align: center;
  border: 1px solid #e4eaec;
  border-radius: 3px;
}
.pricing-list .pricing-header {
  border-bottom: 1px solid #e4eaec;
  border-radius: 3px 3px 0 0;
}
.bg-blue-700 {
  background-color: #409eff !important;
}
.pricing-list .pricing-title {
  padding: 15px 30px;
  font-size: 14px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 2px;
  border-radius: 3px 3px 0 0;
}
.pricing-list .pricing-price {
  padding: 20px 30px;
  margin: 0;
  font-size: 33px;
  font-weight: 700;
  color: #37474f;
}
.pricing-list .pricing-footer {
  padding: 30px;
  border-radius: 0 0 3px 3px;
  text-align: center;
}
.text-left {
  text-align: left !important;
}
.infos-main {
  margin-top: 20px;
}
.infos-footer {
  width: 100;
  text-align: center;
}
</style>
